package cn.rkylin.oms.goods.controller;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.annotation.SystemControllerLog;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.export.IExport;
import cn.rkylin.oms.goods.dao.GoodsDAOImpl;
import cn.rkylin.oms.goods.service.IGoodsCategoryService;
import cn.rkylin.oms.goods.service.IGoodsService;
import cn.rkylin.oms.goods.vo.*;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统商品控制器
 *
 * @author wangxing
 * @version 1.0.0
 * @created 2017-7-5
 */
@Controller
@RequestMapping("/goods")
public class GoodsController extends ApolloController {
    private static final Log logger = LogFactory.getLog(GoodsController.class);
    // 常量定义
    private static final String PAGE_SELECT_STATEMENT = "pageSelectGoods"; // 分页语句Statement名称
    private static final String PAGE_SELECT_STATEMENT_SKU = "pageSelectGoodsSku"; // 分页语句Statement名称
    private static final String GOTO_GOODS_LIST = "/goods/goodsList"; // 跳转到系统商品列表页
    private static final String RESULT_SUCCESS_FLG = "success";
    private static final String RESULT_ERROR_MESSAGE = "errormessage";
    private static final String RESULT_ROW_COUNT = "rowcount";

    /**
     * 系统商品服务层
     */
    @Autowired
    private IGoodsCategoryService goodsCategoryService;

    /**
     * 系统商品服务层
     */
    @Autowired
    private IGoodsService goodsService;

    /**
     * 构造函数
     */
    public GoodsController() {

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        setExportService((IExport) goodsService);
    }

    /**
     * 跳转到系统商品列表
     */
    @RequestMapping(value = "/gotoGoodsList")
    public String gotoItemList() {
        return GOTO_GOODS_LIST;
    }

    /**
     * 跳转到系统商品列表
     *
     * @param quickSearch
     * @param start
     * @param length
     * @param formJson
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/goodsList")
    public Map<String, Object> getGoodsList(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
            @RequestParam(required = false, defaultValue = "10") int length, String formJson) throws Exception {

        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();
        try {
            // 登录与权限检测
            CurrentUser currentUser = logonCheck(returnMap);
            if (currentUser == null) {
                return returnMap;
            }

            // 处理查询条件
            GoodsVO param = new GoodsVO();

            // 前台快速搜索条件
            if (StringUtils.isNotBlank(quickSearch)) {
                quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
                if (StringUtils.isNotEmpty(quickSearch)) {
                    param.setSearchCondition(quickSearch.trim());
                }
            }

            if (StringUtils.isNotBlank(formJson)) {
                // 高级查询检索条件
                formJson = URLDecoder.decode(formJson, "UTF-8");
                Map<String, String> mapItem = JSONObject.parseObject(formJson, Map.class);

                if (mapItem != null) {
                    if (StringUtils.isNotEmpty(mapItem.get("goodsCode"))) {
                        param.setGoodsCode(mapItem.get("goodsCode"));
                    }
                    if (StringUtils.isNotEmpty(mapItem.get("goodsName"))) {
                        param.setGoodsName(mapItem.get("goodsName"));
                    }
                    if (StringUtils.isNotEmpty(mapItem.get("skuCode"))) {
                        param.setSkuCode(mapItem.get("skuCode"));
                    }
                    if (StringUtils.isNotEmpty(mapItem.get("skuName"))) {
                        param.setSkuName(mapItem.get("skuName"));
                    }
                    if (StringUtils.isNotEmpty(mapItem.get("skuMCode"))) {
                        param.setSkuMCode(mapItem.get("skuMCode"));
                    }
                    if (StringUtils.isNotEmpty(mapItem.get("barCode"))) {
                        param.setBarCode(mapItem.get("barCode"));
                    }

                } else {
                    param.setGoodsId("00000000000000000000000000000000");
                }
            }

            String prjId = currentUser.getCurrentProject().getPrjId();
            if (StringUtils.isBlank(prjId)) {
                returnMap.put(JSON_RESULT, FAILED);
                return returnMap;
            }
            param.setPrjId(prjId);

            // 处理转义的字段
            Map<String, String> replaceFieldsMap = new HashMap<String, String>();
            // replaceFieldsMap.put("status", "approveStatus");

            // 排序语句生成
            String orderStatement = getOrderString(GoodsDAOImpl.class.getName(), PAGE_SELECT_STATEMENT, replaceFieldsMap);
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }

            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            // 获取分页数据
            PageInfo<GoodsVO> goodsVOList = goodsService.findByWhere(page, length, param);

            // 设置返回结果内容
            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RECORDS_FILTERED, goodsVOList.getTotal());
            returnMap.put(RECORDS_TOTAL, goodsVOList.getTotal());
            returnMap.put(RETURN_DATA, goodsVOList.getList());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }
        // 生成返回结果json串，null内容也需要返回
        return returnMap;
    }

    @ResponseBody
    @RequestMapping(value = "/goodsSkuList")
    public Map<String, Object> getGoodsSkuList(String goodsId) throws Exception {
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        try {

            GoodsSkuVO param = new GoodsSkuVO();
            if (StringUtils.isNotEmpty(goodsId)) {
                param.setGoodsId(goodsId);
            }

            // 排序语句生成
            String orderStatement = getOrderString(GoodsDAOImpl.class.getName(), PAGE_SELECT_STATEMENT_SKU, null, "BaseResultMapSku");
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }

            List<GoodsSkuVO> goodsSkuVOList = goodsService.findByWhere(param);
            if (!goodsSkuVOList.isEmpty()) {
                returnJSON.put(JSON_RESULT, SUCCESS);
                returnJSON.put(RECORDS_FILTERED, goodsSkuVOList.size());
                returnJSON.put(RECORDS_TOTAL, goodsSkuVOList.size());
                returnJSON.put(RETURN_DATA, goodsSkuVOList);
            } else {
                returnJSON.put(JSON_RESULT, SUCCESS);
                returnJSON.put(RECORDS_FILTERED, 0);
                returnJSON.put(RECORDS_TOTAL, 0);
                returnJSON.put(RETURN_DATA, goodsSkuVOList);
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }
        return returnJSON;
    }

    /**
     * 系统商品数据处理
     *
     * @param shopId
     * @param shopName
     * @param splitType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/updateGoods")
    @SystemControllerLog(module = "系统商品", operation = "系统商品数据处理", keyCode = "goodsId")
    public Map<String, Object> updateGoods(@RequestBody GoodsInfoVO goodsInfo) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }
        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }
        mapParm.put("entId", entId);
        mapParm.put("entName", entName);
        mapParm.put("prjId", currentUser.getCurrentProject().getPrjId());
        mapParm.put("prjName", currentUser.getCurrentProject().getPrjName());
        try {
            Map<String, Object> map = goodsService.modifyGoodsData(goodsInfo, mapParm);
            resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
            resultMap.put(RESULT_ROW_COUNT, map.get("count"));
            resultMap.put(RESULT_ERROR_MESSAGE, map.get("errorMessage"));
            resultMap.put("goodsId", goodsInfo.getGoods().getId());
        }catch (Exception e) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, e.getMessage());
            resultMap.put("goodsId", goodsInfo.getGoods().getId());
        }
        return resultMap;
    }

    /**
     * 系统商品数据处理
     *
     * @param shopId
     * @param shopName
     * @param splitType
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/updateCategorybyGoodsIds")
    @SystemControllerLog(module = "系统商品", operation = "系统商品数据处理", keyCode = "goodsId")
    public Map<String, Object> updateCategorybyGoodsIds(@RequestBody CategoryActionVO categoryActionVO) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        //
        Map<String, Object> map = goodsService.updateCategorybyGoodsIds(categoryActionVO);
        resultMap.put(RESULT_SUCCESS_FLG, map.get("isOk"));
        resultMap.put(RESULT_ROW_COUNT, map.get("count"));
        resultMap.put(RESULT_ERROR_MESSAGE, map.get("errorMessage"));
        resultMap.put("goodsId", categoryActionVO.getGoodsId());
        return resultMap;
    }

    /**
     * updateGoodsIsSnOpen:批量启用商品序列号管理功能开关. <br/>
     *
     * @author zhanghao
     * @param goodsIds 商品ID，逗号分割
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/updateGoodsIsSnOpen")
    @SystemControllerLog(module = "系统商品", operation = "批量开启商品序列号管理功能开关", keyCode = "goodsIds")
    public Map<String, Object> updateGoodsIsSnOpen(String goodsIds) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(JSON_MSG, StringUtils.EMPTY);

        String[] goodsIdArray = goodsIds.split(",");
        for (String goodsId : goodsIdArray) {
            try {
                goodsService.setGoodsIsSn(goodsId, YesNoConst.YES);
            } catch (Exception e) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, String.valueOf(resultMap.get(JSON_MSG)).concat(e.getMessage()+" "));
            }
        }

        if (!FAILED.equals(resultMap.get(JSON_RESULT))) {
            resultMap.put(JSON_RESULT, SUCCESS);
            resultMap.put(JSON_MSG, "启用成功");
        }
        return resultMap;
    }
    
    @ResponseBody
    @RequestMapping(value = "/updateGoodsIsSnClose")
    @SystemControllerLog(module = "系统商品", operation = "批量关闭商品序列号管理功能开关", keyCode = "goodsIds")
    public Map<String, Object> updateGoodsIsSnClose(String goodsIds) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(JSON_MSG, StringUtils.EMPTY);
        String[] goodsIdArray = goodsIds.split(",");
        for (String goodsId : goodsIdArray) {
            try {
                goodsService.setGoodsIsSn(goodsId, YesNoConst.NO);
            } catch (Exception e) {
                resultMap.put(JSON_RESULT, FAILED);
                resultMap.put(JSON_MSG, String.valueOf(resultMap.get(JSON_MSG)).concat(e.getMessage()+" "));
            }
        }
        
        if (!FAILED.equals(resultMap.get(JSON_RESULT))) {
            resultMap.put(JSON_RESULT, SUCCESS);
            resultMap.put(JSON_MSG, "停用成功");
        }
        return resultMap;
    }

    /**
     * 修改画面的数据取得
     *
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getGoodsById")
    public Map<String, Object> getGoodsById(String goodsId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        // 取得系统商品
        GoodsVO goodsVO = StringUtils.isBlank(goodsId) ? null : goodsService.getGoodsById(goodsId);

        // 取得规格
        List<GoodsSkuVO> list = StringUtils.isBlank(goodsId) ? null : goodsService.getGoodsSkuByGoodsId(goodsId);

        returnMap.put("goods", goodsVO);
        returnMap.put("goodsSku", list);
        returnMap.put(JSON_RESULT, SUCCESS);

        return returnMap;
    }

    /**
     * 商品分类数据取得
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getGoodsCategory")
    public Map<String, Object> getGoodsCategory() throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            returnMap.put("goodsCategory", null);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return returnMap;
        }
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put("goodsCategory", null);
            returnMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return returnMap;
        }
        String entId = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
        }

        // 取得商品分类list
        List<GoodsCategoryVO> listGoodsCategory = goodsCategoryService.getAllData(entId);
        List<TreeTableGoodsCategory> list2 = formatGoodsCategory(listGoodsCategory);
        returnMap.put("goodsCategory", list2);
        returnMap.put(JSON_RESULT, SUCCESS);

        return returnMap;
    }

    /**
     * 检索结果集，按照parentid进行分组排序
     *
     * @param listGoodsCategory
     * @return
     */
    private List<TreeTableGoodsCategory> formatGoodsCategory(List<GoodsCategoryVO> listGoodsCategory) {
        if (listGoodsCategory == null || listGoodsCategory.size() <= 0) {
            return null;
        }
        Map<String, List<GoodsCategoryVO>> map = new HashMap<String, List<GoodsCategoryVO>>();

        for (int i = 0; i < listGoodsCategory.size(); i++) {
            GoodsCategoryVO vo = listGoodsCategory.get(i);
            List<GoodsCategoryVO> list = null;
            String mapKey = StringUtils.isNotBlank(vo.getParentCatgId()) ? vo.getParentCatgId() : "0";
            if (map.get(mapKey) == null) {
                list = new ArrayList<GoodsCategoryVO>();
            } else {
                list = map.get(mapKey);
            }
            list.add(vo);
            map.put(mapKey, list);
        }

        return getListGoodsCategory(map, "0", 0L, 1L);

    }

    /**
     * 形成前端认识的数据格式
     *
     * @param map
     * @param parentCatgId
     * @param pId
     * @param id
     * @return
     */
    private List<TreeTableGoodsCategory> getListGoodsCategory(Map<String, List<GoodsCategoryVO>> map, String parentCatgId, long pId, long id) {
        List<TreeTableGoodsCategory> listResult = new ArrayList<TreeTableGoodsCategory>();
        List<GoodsCategoryVO> listGoodsCategoryVO = map.get(parentCatgId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (listGoodsCategoryVO == null || listGoodsCategoryVO.size() <= 0) {
            return null;
        }

        for (int i = 0; i < listGoodsCategoryVO.size(); i++) {
            GoodsCategoryVO vo = listGoodsCategoryVO.get(i);
            TreeTableGoodsCategory goodsCategory = new TreeTableGoodsCategory();
            goodsCategory.setId(id);
            goodsCategory.setpId(pId);
            goodsCategory.setName(vo.getCatgName());
            goodsCategory.setKey(vo.getCatgId());
            goodsCategory.setParentKey(vo.getParentCatgId());
            List<String> tdList = new ArrayList<String>();
            tdList.add(vo.getRemark());
            tdList.add(sdf.format(vo.getCreateTime()));
            tdList.add(sdf.format(vo.getUpdateTime()));

            // 子节点
            List<TreeTableGoodsCategory> listChild = getListGoodsCategory(map, vo.getCatgId(), id, Long.valueOf(id + "1").longValue());
            if (listChild != null && listChild.size() > 0) {
                tdList.add("");
                goodsCategory.setTd(tdList);
                listResult.add(goodsCategory);
                listResult.addAll(listChild);
            } else {
                // tdList.add("<a href='javascript:void(0);'
                // class='returnGoodsCategory' catgId = '"+vo.getCatgId()+"'
                // catgName='"+vo.getCatgName()+"'>选择</a>");
                tdList.add("<button catgId = '" + vo.getCatgId() + "' catgName='" + vo.getCatgName()
                        + "' type='button' class='btn btn-info btn-xs returnGoodsCategory'><i class='fa fa-edit'></i>&nbsp;选择</button>");
                goodsCategory.setTd(tdList);
                listResult.add(goodsCategory);
            }

            id = id + 1L;
        }
        return listResult;
    }

    /**
     * 修改画面的数据取得
     *
     * @param goodsId
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/showSkuList")
    public Map<String, Object> showSkuList(String goodsId) throws Exception {
        // 用于返回值的json对象
        Map<String, Object> returnMap = new HashMap<String, Object>();

        // 取得规格
        List<GoodsSkuVO> list = StringUtils.isBlank(goodsId) ? null : goodsService.getGoodsSkuByGoodsId(goodsId);

        // 设置返回结果内容
        returnMap.put(JSON_RESULT, SUCCESS);
        returnMap.put(RECORDS_FILTERED, list == null ? 0 : list.size());
        returnMap.put(RECORDS_TOTAL, list.size());
        returnMap.put(RETURN_DATA, list);

        return returnMap;
    }

    /**
     * 获取当前项目下的所有系统sku
     *
     * @param quickSearch
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/getAllGoodsSkuList")
    public Map<String, Object> getAllGoodsSkuList(String quickSearch, @RequestParam(required = false, defaultValue = "0") int start,
            @RequestParam(required = false, defaultValue = "10") int length) throws Exception {
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        try {
            // 登录与权限检测
            CurrentUser currUser = logonCheck(returnJSON);
            if (currUser == null) {
                return returnJSON;
            }

            // 处理查询条件
            GoodsSkuVO param = new GoodsSkuVO();

            if (StringUtils.isNotBlank(quickSearch)) {
                quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
                param.setSearchCondition(quickSearch);
            }
            // 排序语句生成
            String orderStatement = getOrderString(GoodsDAOImpl.class.getName(), PAGE_SELECT_STATEMENT_SKU, null, "BaseResultMapSku");
            if (StringUtils.isNotEmpty(orderStatement)) {
                param.setOrderBy(orderStatement);
            }
            // 只显示当前项目下的sku，不要显示其它项目下的
            String prjId = currUser.getCurrentProject().getPrjId();
            param.setPrjId(prjId);

            // 处理分页信息
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;

            PageInfo<GoodsSkuVO> goodsSkuVOList = goodsService.findByWhere(page, length, param);

            returnJSON.put(JSON_RESULT, SUCCESS);
            returnJSON.put(RECORDS_FILTERED, goodsSkuVOList.getTotal());
            returnJSON.put(RECORDS_TOTAL, goodsSkuVOList.getTotal());
            returnJSON.put(RETURN_DATA, goodsSkuVOList.getList());

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }
        return returnJSON;
    }

    /**
     * 查询仓库商品列表
     *
     * @return 结果Map
     * @throws Exception
     *             异常
     */
    @ResponseBody
    @RequestMapping(value = "/getStockSkuList")
    public Map<String, Object> getStockSkuList(String formJson) {

        Map<String, Object> returnMap = new HashMap<String, Object>();

        try {
            formJson = URLDecoder.decode(formJson, "UTF-8");
            SkuStockVO skuStockVO = JSONObject.parseObject(formJson, SkuStockVO.class);
            // CurrentUser currentUser =
            // WebContextFactory.getWebContext().getCurrentUser();
            // String projectId = currentUser.getCurrentProject().getPrjId();
            // skuStockVO.setPrjId(projectId);

            List<SkuStockVO> skuStockVOList = goodsService.selectStockSkuList(skuStockVO);

            returnMap.put(JSON_RESULT, SUCCESS);
            returnMap.put(RETURN_DATA, skuStockVOList);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnMap.put(JSON_RESULT, FAILED);
            returnMap.put(JSON_MSG, ex.getMessage());
        }

        return returnMap;
    }

    /**
     * 删除系统规格 LiPeng 2017-12-27 add
     *
     * @param skuId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteGoods")
    @SystemControllerLog(module = "系统商品", operation = "删除系统规格", keyCode = "skuId")
    public Map<String, Object> deleteGoods(String skuId) {
        // 用于返回值的json对象
        Map<String, Object> returnJSON = new HashMap<String, Object>();
        try {

            Map<String, String> serviceMap = goodsService.checkDelSku(skuId);
            // 当校验成功直接删除
            if (null == serviceMap) {
                goodsService.deleteGoodsSku(skuId);
                returnJSON.put(JSON_RESULT, SUCCESS);
                returnJSON.put(JSON_MSG, "删除成功");
            } else {
                if (serviceMap != null && serviceMap.size() > 0) {
                    if (!"ok".equals(serviceMap.get("error").toString())) {
                        returnJSON.put(JSON_RESULT, FAILED);
                        returnJSON.put(JSON_MSG, serviceMap.get("error"));
                    } else {
                        goodsService.deleteGoodsSku(skuId);
                        returnJSON.put(JSON_RESULT, SUCCESS);
                        returnJSON.put(JSON_MSG, "删除成功");
                    }
                }
            }

        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            returnJSON.put(JSON_RESULT, FAILED);
            returnJSON.put(JSON_MSG, ex.getMessage());
        }
        returnJSON.put("skuId", skuId);
        return returnJSON;
    }

    /**
     * 判断是否有新增，修改
     *
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/isadd")
    @SystemControllerLog(module = "系统商品", operation = "判断按钮", keyCode = "key")
    public Map<String, Object> isadd() throws Exception {

        // 用于返回值的json对象
        Map<String, Object> resultMap = new HashMap<String, Object>();
        CurrentUser currentUser = WebContextFactory.getWebContext().getCurrentUser();
        if (currentUser == null) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户失效");
            return resultMap;
        }

        Map<String, String> mapParm = new HashMap<String, String>();
        Map<String, String> enterpriseMap = currentUser.getEnterpriseMap();
        if (enterpriseMap == null || enterpriseMap.size() == 0) {
            resultMap.put(RESULT_SUCCESS_FLG, "n");
            resultMap.put(RESULT_ERROR_MESSAGE, "当前用户没有企业ID");
            return resultMap;
        }
        String entId = "";
        String entName = "";
        for (Map.Entry<String, String> entry : enterpriseMap.entrySet()) {
            entId = entry.getKey();
            entName = entry.getValue();
        }

        resultMap.put(RESULT_SUCCESS_FLG, "y");
        if ("51f17f6ddbae11e68c4d001e67c7ec3a".equals(entId)) {
            resultMap.put("isadd", "n");
        } else {
            resultMap.put("isadd", "y");
        }

        return resultMap;
    }

    /**
     * 下载
     *
     * @return map response body
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = { "/downLoadGoods" })
    public Map<String, Object> downLoadGoods() throws Exception {

        Map<String, Object> map = goodsService.downLoadGoods("51f17f6ddbae11e68c4d001e67c7ec3a", "");

        return map;
    }
}
